import { getBaseUrl } from '@/services/urlconst';
import { getKeyRouter, getUserDepID, requestFunComponFiex } from '@/utils';
import { useMemo } from 'react';
import { useRequest } from 'umi';
const baseUrl = getBaseUrl();
/**查询所有项目 */
export const getProjectService = () => {
  const request = useRequest(
    (payload: Record<string, any>) => {
      let reqParams: Record<string, any> = {
        current: 1,
        pageSize: 10,
        sort: 'KSRQ',
        order: 'desc',
        BMDM: getUserDepID(),
        // GSDM: getCompanyID(),
        ...payload,
      };
      let reqUrl = `${baseUrl}/purchaseApplyController/getProject`;
      return requestFunComponFiex('POST', reqUrl, reqParams);
    },
    {
      // manual: true,
      paginated: true,
      defaultPageSize: 10,
    },
  );
  const ProjectData = useMemo(() => {
    // return []
    if (request?.data?.rows?.length) {
      const newData: Record<string, any>[] = [];
      request?.data?.rows?.forEach((item) => {
        newData.push({
          ...item,
          projectCode: item?.xmdm,
          projectName: item?.xmmc,
          projectSum: item?.ysje,
          mockID: getKeyRouter(),
        });
      });
      return newData;
    } else {
      return [];
    }
  }, [request?.data?.rows]);

  return {
    getProjectRun: request.run,
    getProjectLoading: request.loading,
    getProjectPagination: request.pagination,
    ProjectData,
  };
};
